<!DOCTYPE html>
<html lang="zh_CN" xmlns="http://www.w3.org/1999/xhtml" xmlns:th="http://www.thymeleaf.org"
      xmlns:shiro="http://www.pollix.at/thymeleaf/shiro">
<head>
    <meta charset="utf-8">
    <title>广告位 AD List</title>
    <meta name="renderer" content="webkit">
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    <meta name="viewport"
          content="width=device-width, initial-scale=1.0, minimum-scale=1.0, maximum-scale=1.0, field-scalable=0">
    <link rel="stylesheet" th:href="@{/statics/layui/css/layui.css}" media="all">
    <link rel="stylesheet" th:href="@{/statics/style/admin.css}" media="all">
</head>
<body>

<div class="layui-fluid">
    <div class="layui-row layui-col-space15">
        <div class="layui-col-md12">
            <div class="layui-card">
                <div class="layui-card-header">广告位列表</div>
                <div class="layui-card-body">
                    <table class="layui-hide" id="ad-list-table" lay-filter="ad-list-filter"></table>

                    <script type="text/html" id="ad-list-table-operate-bar">
                        <a shiro:hasPermission="sys:ad:edit" class="layui-btn layui-btn-xs" lay-event="edit"><i
                                class="layui-icon layui-icon-edit"></i>编辑</a>
                        <a shiro:hasPermission="sys:ad:remove" class="layui-btn layui-btn-danger layui-btn-xs"
                           lay-event="del"><i class="layui-icon layui-icon-delete"></i>删除</a>
                    </script>

                    <script type="text/html" id="ad-list-table-toolbar">
                        <div class="layui-btn-container">
                            <button shiro:hasPermission="sys:ad:add" class="layui-btn layui-btn-sm" lay-event="add">
                                添加广告位
                            </button>
                        </div>
                    </script>

                </div>
            </div>
        </div>
    </div>
</div>


<script th:src="@{/statics/layui/layui.js}"></script>
<script th:inline="none">
    layui.config({
        base: '/statics/' //静态资源所在路径
    }).extend({
        index: 'lib/index' //主入口模块
    }).use(['index', 'table'], function () {
        var $ = layui.$
            , admin = layui.admin
            , table = layui.table;

        var tableIns = table.render({
            elem: '#ad-list-table'
            , url: '/api/ad/list'
            , id: 'id'
            , toolbar: '#ad-list-table-toolbar'
            , cols: [[
                {field: 'id', width: 60, title: '序号'}
                , {field: 'title', title: '广告位', sort: true}
                , {
                    field: 'type', width: 100, title: '类型', sort: true, templet: function (d) {
                        if(d.type == 'image'){
                            return '图片';
                        }else if( d.type == 'video'){
                            return '视频';
                        }else{
                            return '其他';
                        }
                    }
                }
                , {width: 120, title: '尺寸(px)', sort: true, templet: function (d) {
                    return d.width + ' * ' + d.height ;
                 }}
                , {field: 'createTime', width: 180, title: '创建时间', sort: true}
                , {width: 200, align: 'center', fixed: 'right', toolbar: '#ad-list-table-operate-bar'}
            ]]
        });

        //头工具栏事件
        table.on('toolbar(ad-list-filter)', function (obj) {
            var checkStatus = table.checkStatus('id');
            switch (obj.event) {
                case 'add':
                    layer.open({
                        type: 2
                        , title: '添加广告位'
                        , content: '/web/ad/add'
                        , maxmin: true
                        , area: ['600px', '450px']
                        , btn: ['确定', '取消']
                        , yes: function (index, layero) {
                            var iframeWindow = window['layui-layer-iframe' + index]
                                , submitID = 'LAY-ad-add-submit'
                                , submit = layero.find('iframe').contents().find('#' + submitID);

                            //监听提交
                            iframeWindow.layui.form.on('submit(' + submitID + ')', function (data) {
                                var field = data.field; //获取提交的字段

                                //提交 Ajax 成功后，静态更新表格中的数据
                                $.ajax({
                                    url: '/api/ad/add',
                                    method: 'post',
                                    data: data.field,
                                    dataType: 'JSON',
                                    success: function (res) {
                                        if (res.code == '0') {
                                            tableIns.reload(); //数据刷新
                                            layer.close(index); //关闭弹层
                                        } else {
                                            alert(res.msg);
                                        }
                                    },
                                    error: function (err) {
                                    }
                                });

                            });

                            submit.trigger('click');
                        }
                    });
                    break;
            }
            ;
        });

        //监听行工具事件
        table.on('tool(ad-list-filter)', function (obj) {
            var data = obj.data;
            switch (obj.event) {
                case 'del':
                    layer.confirm('真的删除么', function (index) {
                        $.ajax({
                            url: '/api/ad/removeFlag?id=' + data.id,
                            method: 'put',
                            contentType: "application/json;charset=UTF-8",
                            dataType: 'JSON',
                            success: function (res) {
                                if (res.code == '0') {
                                    layer.msg('记录已删除!', {icon: 1, time: 1000}, function () {
                                        obj.del();
                                        layer.close(index); //关闭弹层
                                    });
                                } else {
                                    layer.msg(res.msg);
                                }
                            },
                            error: function (err) {
                            }
                        });
                    });
                    break;
                case 'edit':
                    layer.open({
                        type: 2
                        , title: '编辑广告位'
                        , content: '/web/ad/edit?id=' + data.id
                        , maxmin: true
                        , area: ['600px', '450px']
                        , btn: ['确定', '取消']
                        , yes: function (index, layero) {
                            var iframeWindow = window['layui-layer-iframe' + index]
                                , submitID = 'LAY-ad-add-submit'
                                , submit = layero.find('iframe').contents().find('#' + submitID);

                            //监听提交
                            iframeWindow.layui.form.on('submit(' + submitID + ')', function (data) {
                                var field = data.field; //获取提交的字段

                                //提交 Ajax 成功后，静态更新表格中的数据
                                $.ajax({
                                    url: '/api/ad/edit',
                                    method: 'post',
                                    data: data.field,
                                    dataType: 'JSON',
                                    success: function (res) {
                                        if (res.code == '0') {
                                            tableIns.reload(); //数据刷新
                                            layer.close(index); //关闭弹层
                                        } else {
                                            alert(res.msg);
                                        }
                                    },
                                    error: function (err) {
                                    }
                                });

                            });

                            submit.trigger('click');
                        }
                    });
                    break;
            }
        });

    });
</script>
</body>
</html>